package q530_getMinimumDifference;

import CommonClass.Common.TreeNode;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

public class Solution_1 {
    // 暴力搜索 中序遍历后将所有的节点排列起来找最小差值
    public int getMinimumDifference(TreeNode root) {
        List<Integer> res = new ArrayList<Integer>();
        Deque<TreeNode> stack = new LinkedList<TreeNode>();
        TreeNode node = root;
        while (!stack.isEmpty() || node != null){
            // 不停的将左子节点加入栈中
            while (node != null) {
                stack.push(node);
                node = node.left;
            }
            node = stack.pop();
            res.add(node.val);
            node = node.right;
        }
        int minVal = Integer.MAX_VALUE;
        for (int i = 1; i < res.size(); i++) {
            minVal = Math.min(minVal, res.get(i) - res.get(i - 1));
        }
        return minVal;

    }
}
